package com.life.wd.controller;

import com.life.wd.dto.SupplierDTO;
import com.life.wd.dto.SupplierUpdateDTO;
import com.life.wd.entity.Supplier;
import com.life.wd.result.PageResult;
import com.life.wd.result.Result;
import com.life.wd.service.SupplierService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/supplier")
@Slf4j
@Api(tags = "供应商管理")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 添加供应商
     */
    @PostMapping("/add")
    public Result addSupplier(@RequestBody Supplier supplier) {
        log.info("添加供应商：{}", supplier);
        try {
            int result = supplierService.addSupplier(supplier);
            if (result > 0) {
                return Result.success("添加供应商成功");
            } else {
                return Result.error("添加供应商失败");
            }
        } catch (Exception e) {
            log.error("添加供应商异常", e);
            return Result.error("添加供应商异常，请联系管理员");
        }
    }

    /**
     * 获取供应商
     */
    @PostMapping("/list")
    public Result listSupplier(@RequestBody SupplierDTO supplierDTO) {
        log.info("获取供应商: {}",supplierDTO);
        try {
            PageResult pageResult = supplierService.listSupplier(supplierDTO);
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("获取所有供应商异常", e);
            return Result.error("获取所有供应商异常，请联系管理员");
        }
    }

    /**
     * 更新供应商信息
     */
    @PutMapping("/{id}")
    public Result<?> updateSupplier(@PathVariable Integer id, @RequestBody SupplierUpdateDTO updateDTO) {
        supplierService.updateSupplier(id, updateDTO);
        return Result.success("更新供应商成功");
    }

    /**
     * 删除供应商
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteSupplier(@PathVariable Integer id) {
        supplierService.deleteSupplier(id);
        return Result.success("删除供应商成功");
    }
}